package artifacts;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.Timer;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import c4jason.*;
import jason.architecture.*;
import jason.asSemantics.*;
import jason.asSyntax.*;

import java.util.*;
import java.io.*;

import c4jason.CAgentArch;

import cartago.*;

public class FileSystemInterfaceArt extends Artifact {
	
	public DefaultListModel model = new DefaultListModel();
	private String path = "D:/Archivos";
	public Timer timer;
	private ObsProperty prop;
	private boolean checking;

	void init(){
	
		defineObsProperty("newDocument",-1);
		defineObsProperty("document",-1);
		cargarArchivos(path);
		checking = false;		
	}
	
	@OPERATION void arrancar(String vacio)
	{
		timer = new Timer (5000, new ActionListener () 
		{ 
		    public void actionPerformed(ActionEvent e) 
		    { 
		    	chequearCambios(path);
		     } 
		});
		
		timer.start();	
	}
	
	@OPERATION void start(String vacio){
		checking = true;
		this.execInternalOp("checkingProcess",vacio);
	}

	@OPERATION void stop(){
		checking = false;
	}

	@INTERNAL_OPERATION void checkingProcess(String vacio)
	{
		while (checking){
			chequearCambios(path);
			await_time(5000);
		}
	}
	
	@OPERATION Boolean areNewDocument(){
		/*
		if(ReqID > -1)
		{
			ObsProperty prop = getObsProperty("getRequiredDocument");
			prop.updateValue(0,123);
			prop.updateValue(1,"lalalal");
			prop.updateValue(2,"dominio");
		}*/
		return false;
	}
	
	private void cargarArchivos(String dir)
	{				
		try
		{
		File directorio = new File(dir);
        String[] listaDirectorio = directorio.list();
		for (int i=0; i<listaDirectorio.length; i++) {
		    model.add(i, listaDirectorio[i]);			
		}
		}
		catch(Exception e)
		{
			System.out.println("No existe el directorio o es inaccesible");
		}
	}
	
	public void chequearCambios(String dir)
	{
		prop = getObsProperty("newDocument");
		
		try
		{
		File directorio = new File(dir);
        String[] listaDirectorio = directorio.list();
		for (int i=0; i<listaDirectorio.length; i++) {
			if(!model.contains(listaDirectorio[i]))
			{
				prop.updateValue(i);
				JOptionPane.showMessageDialog(null, "Nuevo archivo encontrado: " + listaDirectorio[i] + " prop: " + prop);
				model.add(i, listaDirectorio[i]);
				
			}
		}
		}
		catch(Exception e)
		{
			System.out.println("No existe el directorio o es inaccesible");
		}
	}
	
	@OPERATION void readDocument(int document_id)
	{
		File file;
		FileReader fr;
		BufferedReader br;
		String line = "";
		String content = "";
		ObsProperty prop = getObsProperty("document");
		
		try{		
			file = new File (path + "/" +model.get(document_id));
			fr = new FileReader (file);
			br = new BufferedReader(fr);
			line = br.readLine();
						
			content += line;
		
			while((line = br.readLine())!= null)
			{
				content += line;
			}

			prop.updateValue(content);
			//CAgentArch.getTS().getAg().addBel(Literal.parseLiteral("document(\""+content+"\")"));
		}
		catch(Exception e){
			System.out.println("Error "+e);
		}
	}
	
}
